$(function () {
    var dateArray = [] // 已经签到的
    var bj=false//标记
    var month = new Date().getMonth()+1;//月份
    var url = "../qiandao"
    var date = {"method": "selectDate","async":"false"}
    $.post(url, date, function (result) {
        $(result).each(function (i, n) {
            dateArray[i] = n.date.date
            if (n.date.month+1!=month) {
                bj=true
            }
        })
        if (bj) {
            var url = "../qiandao"
            var date = {"method": "upDatabaseDate","async":"false"}
            $.post(url,date,function () {})//更新
        }
        var signFun = function () {
             $('#tian').html(num2);//连续签到
            var $dateBox = $("#js-qiandao-list"),
                $currentDate = $(".current-date"),
                $qiandaoBnt = $("#js-just-qiandao"),
                _html = '',
                _handle = true,
                myDate = new Date();
            // var num = 0;//签到总计次数
            // var num2 = totalDay-dateArray.length;//未签到总计次数

            //当前年月日
            var Ymd = myDate.getFullYear() + '年' + parseInt(myDate.getMonth() + 1) + '月' + myDate.getDate() + '日';
            $currentDate.text(Ymd);

            var monthFirst = new Date(myDate.getFullYear(), parseInt(myDate.getMonth()), 1).getDay();
            var d = new Date(myDate.getFullYear(), parseInt(myDate.getMonth() + 1), 0);
            var totalDay = d.getDate(); //获取当前月的天数

            var num;//签到总计次数
            var num2 = totalDay - dateArray.length;//未签到总计次数

            //连续签到

            $('#by').html(dateArray.length);//显示本月签到总数
            $('#duan').html(num2);//显示断连次数
            rr()
            function rr() {
                var num=0
                    for (var i = dateArray.length-1; i >= 0; i--) {
                        if (myDate.getDate()==dateArray[dateArray.length-1]||myDate.getDate()-1==dateArray[dateArray.length-1]){
                            if (dateArray[i]==dateArray[i-1]+1){
                                num++
                            } else {
                                break
                            }
                        }else {
                            break
                        }
                    }
                if (myDate.getDate()==dateArray[dateArray.length-1]||myDate.getDate()-1==dateArray[dateArray.length-1]){
                    $('#lx').html(num+1);//连续签到
                }else {
                    $('#lx').html(0);//连续签到
                }
                $(result).each(function (i,n) {
                    var sj=myDate.getFullYear()+"."+parseInt(myDate.getMonth() + 1)+"."+n.date.date
                    $("#date_table").append("<tr><td>"+sj+"</td><td>暂无</td></tr>")
                })
            }



            // $('.qiandao-history-inf h4').html(i);//显示签到总数
            // $('.qiandao-history-inf h4:last').html(num2);//显示断连次数
            // $('.qiandao-history-table td:first').html(Ymd);//显示签到日期
            for (var i = 0; i < 42; i++) {
                _html += ' <li><div class="qiandao-icon"></div></li>'
            }
            $dateBox.html(_html) //生成日历网格

            var $dateLi = $dateBox.find("li");
            for (var i = 0; i < totalDay; i++) {

                $dateLi.eq(i + monthFirst).addClass("date" + parseInt(i + 1));//****
                // alert(dateArray.length)
                for (var j = 0; j < dateArray.length; j++) {
                    if (i == dateArray[j]) {
                        $dateLi.eq(i - 1 + monthFirst).addClass("qiandao");
                    }
                }
            } //生成当月的日历且含已签到

            $(".date" + myDate.getDate()).addClass('able-qiandao');

            $dateBox.on("click", "li", function () {
                if ($(this).hasClass('able-qiandao') && _handle) {
                    $(this).addClass('qiandao');
                    qiandaoFun();
                }
            }) //签到

            $qiandaoBnt.on("click", function () {
                if (_handle) {
                    // $('.qiandao-history-inf h4').html(num);//显示签到总数
                    // $('.qiandao-history-inf h4:last').html(num2);//显示断连次数
                    num++;
                    // console.log(     $('.qiandao-history-table td:first'));
                    $('.qiandao-history-table td:first').html(Ymd);//显示签到日期
                    qiandaoFun();
                }
            }); //签到

            var url = "../qiandao"
            var date2 = {"method": "findQianDaoByUidDate","async":"false"}
            $.post(url, date2, function (i) {
                if (i == 1) {
                    $qiandaoBnt.addClass('actived');//签到图片更改
                    _handle = false;
                }
            });//判断今天是否签到，如果签到则修改图片并 _handle = false

            function qiandaoFun() {
                var url = "../qiandao"
                var date3 = {"method": "addQianDao","async":"false"}
                $.post(url, date3, function () {

                });//签到方法，进入数据库
                $qiandaoBnt.addClass('actived');//签到图片更改
                //
                var num=0
                for (var i = dateArray.length-1; i >= 0; i--) {
                    if (myDate.getDate()==dateArray[dateArray.length-1]||myDate.getDate()-1==dateArray[dateArray.length-1]){
                        if (dateArray[i]==dateArray[i-1]+1){
                            num++
                        } else {
                            break
                        }
                    }else {
                        break
                    }
                }
                if (myDate.getDate()==dateArray[dateArray.length-1]||myDate.getDate()-1==dateArray[dateArray.length-1]){
                    $('#tian').html(num+2);//连续签到
                }else {
                    $('#tian').html(1)
                }

                openLayer("qiandao-active", qianDao);//签到弹框
                _handle = false;
            }

            function qianDao() {
                $(".date" + myDate.getDate()).addClass('qiandao');//日历框添加默认样式
            }

        }();

        function openLayer(a, Fun) {
            xx()
            $('.' + a).fadeIn(Fun);
            // console.log(222);
        } //打开弹窗

        var closeLayer = function () {
            $("body").on("click", ".close-qiandao-layer", function () {//事件委托 动态框
                $(this).parents(".qiandao-layer").fadeOut()
            })
        }() //关闭弹窗

        $("#js-qiandao-history").on("click", function () {
            openLayer("qiandao-history-layer", myFun);

            function myFun() {
                // console.log(1)
            } //打开弹窗返回函数
        })
    }, "json");//返回签到了哪几天
    function xx() {
        var url = "../qiandao"
        var date1 = {"method": "userQianDaoNum","async":"false"}
        $.get(url, date1, function (i) {
            $('#zong').html(i);//显示签到总数
        });
    }
})
